package com.wxw.BaseLibrary.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by david on 2016/4/21.
 */
public class SToast {
    public static int BackgroundResourceID = -1;
    public static int TextViewID = -1;
    public static int TextColor = -1;
    public static int Gravity = android.view.Gravity.BOTTOM;
    public static int Gravity_OffSet = 200;
    public static int ShowTime = 1000;
    public static int Animation = -1;

    public static void showToast(Context context, CharSequence text) {
        showToast(context, text, ShowTime);
    }

    public static void showToast(Context context, CharSequence text, int duration) {
        showToast(context, text, Animation, duration);

//        Toast sToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
//        sToast.setGravity(Gravity, 0, Gravity_OffSet);
//        sToast.setDuration(duration);
//        View view = getViewFromResourceID(context, text);
//        if (view != null) {
//            sToast.setView(view);
//        }
////        if (BackgroundResourceID >= 0) {
////            View view = LayoutInflater.from(context).inflate(BackgroundResourceID, null);
////            ViewGroup viewGroup = (ViewGroup)view;
////            if (view != null && viewGroup.getChildCount() > 0) {
////                for (int i=0;i<viewGroup.getChildCount();i++) {
////                    if (viewGroup.getChildAt(i) instanceof TextView) {
////                        ((TextView)viewGroup.getChildAt(i)).setText(text);
////                        if (TextColor >= 0)
////                            ((TextView)viewGroup.getChildAt(i)).setTextColor(TextColor);
////                    }
////                }
////            }
////            sToast.setView(view);
////        } else {
////            sToast.setText(text);
////        }
//        sToast.show();
    }

    public static void showToast(Context context, CharSequence text, int animID, int duration) {
        ExToast exToast = ExToast.makeText(context, text, duration)
                .setAnimations(animID).setGravity(Gravity, 0, Gravity_OffSet)
                .setDuration(duration);
        View view = getViewFromResourceID(context, text);
        if (view != null) {
            exToast.setView(view);
        }
        exToast.show();
    }

    private static View getViewFromResourceID(Context context, CharSequence text) {
        if (BackgroundResourceID >= 0) {
            View view = LayoutInflater.from(context).inflate(BackgroundResourceID, null);
            if (TextViewID >= 0) {
                TextView textView = (TextView) view.findViewById(TextViewID);
                if (textView != null) {
                    textView.setText(text);
                    if (TextColor >= 0) {
                        textView.setTextColor(TextColor);
                    }
                }
            }
//            ViewGroup viewGroup = (ViewGroup)view;
//            if (view != null && viewGroup.getChildCount() > 0) {
//                for (int i=0;i<viewGroup.getChildCount();i++) {
//                    if (viewGroup.getChildAt(i) instanceof TextView) {
//                        ((TextView)viewGroup.getChildAt(i)).setText(text);
//                        if (TextColor >= 0)
//                            ((TextView)viewGroup.getChildAt(i)).setTextColor(TextColor);
//                    }
//                }
//            }
            return view;
        } else {
            return null;
        }
    }
}
